//
//  ViewController.m
//  AnimationTest
//
//  Created by Min on 2024/9/7.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController





- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];
    // 创建一个黑色的view作为背景
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    blackView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:blackView];

    // 定义多个星星的位置
    NSArray *positions = @[[NSValue valueWithCGPoint:CGPointMake(50, 50)],
                           [NSValue valueWithCGPoint:CGPointMake(150, 50)],
                           [NSValue valueWithCGPoint:CGPointMake(50, 150)],
                           [NSValue valueWithCGPoint:CGPointMake(150, 150)],
                           [NSValue valueWithCGPoint:CGPointMake(55, 55)],
                           [NSValue valueWithCGPoint:CGPointMake(45, 35)],
                           [NSValue valueWithCGPoint:CGPointMake(80, 45)],
                           [NSValue valueWithCGPoint:CGPointMake(60, 45)],
    ];

    // 在每个位置创建星星
    for (NSValue *positionValue in positions) {
        CGPoint center = [positionValue CGPointValue];
        CGFloat radius = 5.0;  // 控制星星大小，半径为5像素，总直径为10像素

        // 创建CAShapeLayer
        CAShapeLayer *starLayer = [CAShapeLayer layer];
        starLayer.fillColor = [UIColor whiteColor].CGColor;
        starLayer.lineJoin = kCALineJoinRound;

        // 创建四角星星的路径
        UIBezierPath *starPath = [UIBezierPath bezierPath];
        CGFloat angle = M_PI / 4.0;

        for (int i = 0; i < 8; i++) {
            CGFloat currentAngle = angle * i;
            CGFloat pointRadius = (i % 2 == 0) ? radius : radius / 2.0;
            CGPoint point = CGPointMake(center.x + pointRadius * cos(currentAngle),
                                        center.y + pointRadius * sin(currentAngle));
            if (i == 0) {
                [starPath moveToPoint:point];
            } else {
                [starPath addLineToPoint:point];
            }
        }
        [starPath closePath];

        starLayer.path = starPath.CGPath;

        // 添加闪烁动画
        CABasicAnimation *blinkAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        blinkAnimation.fromValue = @1.0;
        blinkAnimation.toValue = @0.0;
        blinkAnimation.duration = 0.1;
        blinkAnimation.autoreverses = YES; // 动画反转
        blinkAnimation.repeatCount = HUGE_VALF; // 无限循环

        [starLayer addAnimation:blinkAnimation forKey:@"blinkAnimation"];

        // 添加CAShapeLayer到黑色视图的layer
        [blackView.layer addSublayer:starLayer];
    }
}



@end
